package com.project;

import com.project.config.SpringConfig;
import com.project.servlet.TestServlet;
import org.apache.catalina.Context;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public Main(){
        ApplicationContext text = new AnnotationConfigApplicationContext(SpringConfig.class);

        Tomcat tomcat = new Tomcat();
        //设置tomcat端口
        tomcat.setPort(8088);
        //引导http引擎
        tomcat.getConnector();

        //定义上下文
        Context context =tomcat.addContext("",null);
        //注册servlet,第一个参数为上下文对象，第二个参数为servlet名称，第三个参数为servlet实现类对象
        Wrapper wp = Tomcat.addServlet(context,"test",text.getBean(TestServlet.class));
        //容器一启动就初始化
        wp.setLoadOnStartup(1);
        //配置servlet的访问路径
        wp.addMapping("/abc");
        try {
            //启动tomcat
            tomcat.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}
